var express = require('express');
var router = express.Router();
var session = require('express-session');
var moment = require('moment');
var async = require('async');


var db = require('./../../utils/db.js');


router.get('/index', function (req, res, next) {
    res.render('daily/schedule/index');
});

//获取详情
router.get('/calendarData', function (req, res, next) {
    var sql = "select * from schedule where 1=1";
    db.query(sql, function (err, rows) {
        if (!err && rows.length >= 0) {
            var dataList = [];
            for(var i=0; i<rows.length; i++) {
                dataList.push({
                    id: rows[i].id,
                    title: rows[i].task_title,
                    start: rows[i].start_time,
                    end:rows[i].end_time,
                })
            }
            res.json(dataList);
            console.log(dataList)
        }
    });
});

// 增加
router.get('/add', function (req, res, next) {
    var userSql = "select * from base_user";
    db.query(userSql, function (err, rows) {
        if (!err) {
            res.render('daily/schedule/edit', {
                schedule: {
                    id: '',
                    transactor: '',
                    task_status: '',
                    task_title: '',
                    task_description: '',
                    start_time: '',
                    end_time: '',
                },
                userList: rows
            });
        }
    });
});

router.post('/save', function (req, res, next) {
    var transactor = req.body.transactor ? req.body.transactor.trim() : '';
    var task_status = req.body.task_status ? req.body.task_status.trim() : '';
    var task_title = req.body.task_title ? req.body.task_title.trim() : '';
    var task_description = req.body.task_description ? req.body.task_description.trim() : '';
    var start_time = req.body.start_time ? req.body.start_time.trim() : '';
    var end_time = req.body.end_time ? req.body.end_time.trim() : '';
    var id = req.body.id;
    var sql = '';

    if (id) {
        sql = "update schedule set transactor_id = '" + transactor + "',task_status = '" + task_status + "',task_title = '" + task_title + "',task_description = '" + task_description + "',start_time = '" + start_time + "',end_time = '" + end_time + "' where id =  '" + id + "'";
    } else {
        sql = "insert into schedule(transactor_id,task_status,task_title,task_description,start_time,end_time) values('" + transactor + "','" + task_status + "','" + task_title + "','" + task_description + "','" + start_time + "','" + end_time + "')";
    }
    db.query(sql, function (err, rows) {
        if (rows) {
            res.json({
                id: rows.insertId,
                transactor: transactor,
                task_status: task_status,
                task_title: task_title,
                task_description: task_description,
                start_time: start_time,
                end_time: end_time
            });
        } else {
            res.send("保存失败" + err);
        }
    });

});

// 编辑
router.get('/modify/:id', function (req, res, next) {
    var id = req.params.id;
    // var sql = 'select * from schedule where id =  + id';
    var sql = "select * from schedule where id = " + id;
    if(id){
        db.query(sql, function(err,rows){
            if(!err && rows.length > 0){
                var userSql = "select * from base_user";
                db.query(userSql, function (userErr, userRows) {
                    if (!userErr && userRows.length > 0) {
                        res.render('daily/schedule/edit', {
                            schedule: {
                                id: rows[0].id,
                                transactor: rows[0].transactor_id,
                                task_status: rows[0].task_status,
                                task_title: rows[0].task_title,
                                task_description: rows[0].task_description,
                                start_time: rows[0].start_time,
                                end_time: rows[0].end_time
                            },
                            userList: userRows
                        });
                    } else {
                        res.render('daily/schedule/edit', {
                            schedule: {
                                id: rows[0].id,
                                transactor: rows[0].transactor_id,
                                task_status: rows[0].task_status,
                                task_title: rows[0].task_title,
                                task_description: rows[0].task_description,
                                start_time: rows[0].start_time,
                                end_time: rows[0].end_time
                            },
                            userList: []
                        });
                    }
                });

            } else {
                res.render('daily/schedule/edit', {
                    schedule: {
                        id: '',
                        transactor: '',
                        task_status: '',
                        task_title: '',
                        task_description: '',
                        start_time: '',
                        end_time: ''
                    },
                    userList: []
                });
            }
        });
    }

});



//删除
router.get("/delete/:id",function(req,res){
    var id = req.params.id;
    var sql = "delete from schedule where id =  '" + id + "'";
    db.query(sql,function(err,rows){
        if(err){
            res.send("删除失败"+err);
        }
        else {
            res.redirect("/daily/schedule/index");
        }
    });
});

module.exports = router;